<template>
  <el-dialog
    title= '生成代码'
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="作者" prop="author">
      <el-input v-model="dataForm.author" placeholder="请输入作者"></el-input>
    </el-form-item>
    <el-form-item label="备注" prop="comments">
      <el-input v-model="dataForm.comments" placeholder="请输入备注"></el-input>
    </el-form-item>
    <el-form-item label="模块名称" prop="moduleName">
      <el-input v-model="dataForm.moduleName" placeholder="请输入模块名称"></el-input>
    </el-form-item>
    <el-form-item label="包名" prop="packageName">
      <el-input v-model="dataForm.packageName" placeholder="请输入包名"></el-input>
    </el-form-item>
    <el-form-item label="库名" prop="storehouseName">
      <el-input v-model="dataForm.storehouseName" placeholder="请输入库名"></el-input>
    </el-form-item>
    <el-form-item label="表名" prop="tableName">
      <el-input v-model="dataForm.tableName" placeholder="请输入表名"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
  export default {
    data () {
      return {
        visible: false,
        dataForm: {
        author:'',
        comments:'',
        moduleName:'',
        packageName:'',
        storehouseName:'',
        tableName:''
        },
        dataRule: {
          author: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          comments: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          moduleName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          packageName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          storehouseName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
          tableName: [
            { required: true, message: '不能为空', trigger: 'blur' }
          ],
        }
      }
    },
    methods: {
      init (row) {
        console.log("row",row);
        this.visible = true
        this.dataForm.tableName=row.tableName
        this.dataForm.comments=row.tableComment
        this.dataForm.storehouseName=row.tableSchema
        console.log(this.dataForm);
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`codegen/generator/code`),
              method: 'post',
              data: this.$http.adornData(this.dataForm),
              responseType: 'blob' 
            }).then(({data}) => {
                const blog=new Blob([data],{type:"application/octet-stream"})
                const fileName=this.dataForm.tableName+".zip"
                const elink = document.createElement('a');
                elink.download = fileName;
                elink.style.display = 'none';
                elink.href = URL.createObjectURL(blog);
                document.body.appendChild(elink);
                elink.click();
                URL.revokeObjectURL(elink.href); // 释放URL 对象
                document.body.removeChild(elink);
                this.visible = false
            })
          }
        })
      }
    }
  }
</script>
